package spring.yongongsystem.controller.merchant;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import spring.yongongsystem.POJO.dto.MerchantDTO;
import spring.yongongsystem.POJO.entity.Merchant;
import spring.yongongsystem.POJO.vo.MerchantLoginVO;
import spring.yongongsystem.constant.JwtClaimsConstant;
import spring.yongongsystem.properties.JwtProperties;
import spring.yongongsystem.result.Result;
import spring.yongongsystem.service.MerchantService;
import spring.yongongsystem.utils.JwtUtil;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController("merchantMerchantsController")
@RequestMapping("/merchant/merchant")
@Api(tags = "商家相关接口")
@Slf4j
public class MerchantController {

    String key = "merchant_token";
    @Resource
    private RedisTemplate redisTemplate;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private JwtProperties jwtProperties;
    /**
     * 登录
     *
     * @param merchantDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "商家登录")
    public Result<MerchantLoginVO> login(@RequestBody MerchantDTO merchantDTO) {
        log.info("商家登录：{}",merchantDTO);

        Merchant merchant = merchantService.login(merchantDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, merchant.getMerchantId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        MerchantLoginVO merchantLoginVO = MerchantLoginVO.builder()
                .id(merchant.getMerchantId())
                .name(merchant.getName())
                .userName(merchant.getUsername())
                .phone(merchant.getPhone())
                .token(token)
                .build();
        redisTemplate.opsForValue().set(key,token);
        return Result.success(merchantLoginVO);
    }

    /**
     * 新增商家
     * @param merchantDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增商家")
    public Result save(@RequestBody MerchantDTO merchantDTO){
        log.info("新增商家：{}",merchantDTO);
        merchantService.save(merchantDTO);
        return Result.success();
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("商家退出")
    public Result<String> logout() {
        return Result.success();
    }
    /**
     * 编辑商家信息
     * @param merchantDTO
     * @return
     */
    @PutMapping
    @ApiOperation("编辑商家信息")
    public Result update(@RequestBody MerchantDTO merchantDTO){
        log.info("编辑商家信息：{}", merchantDTO);
        merchantService.update(merchantDTO);
        return Result.success();
    }
}
